package internationalenergytradingplatform.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import internationalenergytradingplatform.constant.StatusConstant;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.EmployeeMapper;
import internationalenergytradingplatform.dto.EmployeeDTO;
import internationalenergytradingplatform.dto.EmployeePageQueryDTO;
import internationalenergytradingplatform.pojo.Employee;
import internationalenergytradingplatform.dao.SupplierAccountDao;
import internationalenergytradingplatform.result.PageResult;
import internationalenergytradingplatform.service.SupplierEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SupplierEmployeeServiceImpl implements SupplierEmployeeService {

    @Autowired
    private SupplierAccountDao supplierAccountDao;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void addEmployee(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //使用对象属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);

        //设置账号的状态为正常
        employee.setAccountStatus(StatusConstant.ENABLE);

        //设置创建和修改时间
        employee.setFinalOperatorTime(LocalDateTime.now().toString());

        //设置添加者和最后修改者的id
        employee.setFinalOperator(BaseContext.getCurrentId());
        employee.setPeopleAdd(BaseContext.getCurrentId());

        supplierAccountDao.addEmployee(employee);


    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/4 19:18
     分页查询
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        //开始分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        long total = page.getTotal();
        List<Employee> records = page.getResult();
        return new PageResult(total, records);
    }
}
